package cn.com.se.sharepictrue.adapter;

import android.content.Context;
import android.graphics.PorterDuff;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import cn.com.se.sharepictrue.R;
import cn.com.se.sharepictrue.bean.HomeShareBean;
import cn.com.se.sharepictrue.business.ImageLoader;
import cn.com.se.sharepictrue.quickadapter.BaseAdapterHelper;
import cn.com.se.sharepictrue.quickadapter.MultiViewTypeSupport;
import cn.com.se.sharepictrue.quickadapter.QuickAdapter;
import cn.com.se.sharepictrue.utils.StringUtils;
import cn.com.se.sharepictrue.widget.AdapterGridLayout;

/**
 * Created by KIDNG on 2015/11/30.
 */
public class ShareListAdapter extends QuickAdapter<HomeShareBean> {
    private Context mContext;

    public ShareListAdapter(Context context, int layoutResId) {
        super(context, layoutResId);
        mContext = context;
    }

    public ShareListAdapter(Context context, int layoutResId, List<HomeShareBean> data) {
        super(context, layoutResId, data);
        mContext = context;
    }

    public ShareListAdapter(Context context, List<HomeShareBean> data, MultiViewTypeSupport<HomeShareBean> multiViewTypeSupport) {
        super(context, data, multiViewTypeSupport);
        mContext = context;
    }

    @Override
    protected void convert(final BaseAdapterHelper helper, final HomeShareBean item) {
        helper.setText(R.id.tv_item_share_name, item.getUserName());
        helper.setText(R.id.tv_item_share_time, item.getAddTime());
        helper.setText(R.id.tv_item_share_content, item.getShareContent());
        helper.setText(R.id.tv_item_share_comment, item.getCommentNum() + "");
        helper.setText(R.id.tv_item_share_praise, item.getPraiseNum() + "");
        helper.setText(R.id.tv_item_share_signature,item.getSignature());
        AdapterGridLayout pictrues = helper.getView(R.id.gv_item_share_many_img);
        List<String> imgUrls = item.getImgs();
        if (imgUrls != null && imgUrls.size() > 0) {
            pictrues.setVisibility(View.VISIBLE);
            ShareImgAdapter adapter = new ShareImgAdapter(mContext, imgUrls);
            pictrues.setAdapter(adapter);
        } else {
            pictrues.setVisibility(View.GONE);
        }
        //==============头像   跳转=====================
        ImageView avatar = helper.getView(R.id.iv_item_share_avatar);
        if(!StringUtils.isEmpty(item.getAvatar()))
            ImageLoader.getInstance(mContext).load(avatar,item.getAvatar(),R.mipmap.bg_drawer);
        avatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null)
                    mListener.avatarClick(helper, item);
            }
        });
        //===============收藏分享==================
        TextView collectionView = helper.getView(R.id.tv_item_share_collection);
        if (!StringUtils.isEmpty(item.getFavoriteStatus())&&
                item.getFavoriteStatus().equals(HomeShareBean.HOME_SHARE_IS_FAVORITE))
            collectionView.setText(mContext.getResources().getString(R.string.item_share_collection_selected));
        else
            collectionView.setText(mContext.getResources().getString(R.string.item_share_collection_normal));
        collectionView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null)
                    mListener.collectionClick(helper, item);
            }
        });

        //==============关注用户=======================
        ImageView concernView = helper.getView(R.id.iv_item_share_collection);
        if (item.getConcernUserId() != null && !item.getConcernUserId().equals(HomeShareBean.HOME_SHARE_NO_CONCERN))
            concernView.setColorFilter(mContext.getResources().getColor(R.color.color_item_share_collection),
                    PorterDuff.Mode.SRC_ATOP);//position error
        else
            concernView.clearColorFilter();
        concernView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null)
                    mListener.concernClick(helper, item);
            }
        });

        //===============点赞==========================
        TextView praiseView = helper.getView(R.id.tv_item_share_praise);
        praiseView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null)
                    mListener.praiseClick(helper, item);
            }
        });

        //==================评论======================
        TextView comment = helper.getView(R.id.tv_item_share_comment);
        comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null)
                    mListener.commentClick(helper, item);
            }
        });
    }


    public void setShareItemStatusListener(ShareItemStatusListener listener) {
        mListener = listener;
    }

    private ShareItemStatusListener mListener;

    public interface ShareItemStatusListener {
        void avatarClick(BaseAdapterHelper helper, HomeShareBean item);

        void concernClick(BaseAdapterHelper helper, HomeShareBean item);

        void praiseClick(BaseAdapterHelper helper, HomeShareBean item);

        void collectionClick(BaseAdapterHelper helper, HomeShareBean item);

        void commentClick(BaseAdapterHelper helper, HomeShareBean item);
    }
}
